﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using JinianNet.JNCms.Data;

namespace JinianNet.JNCms.SQLServerDAL
{
    public class Template : BaseDAL, ITemplate
    {
        public int Insert(Models.Template model)
        {
            if (model.ID <= 0)
                model.ID = Db.CreateKey("Template");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,8),
					new SqlParameter("@Key", SqlDbType.VarChar,100),
					new SqlParameter("@Title", SqlDbType.VarChar,200),
					new SqlParameter("@Author", SqlDbType.VarChar,100),
					new SqlParameter("@Version", SqlDbType.VarChar,100),
					new SqlParameter("@Url", SqlDbType.VarChar,150),
					new SqlParameter("@SystemVersion", SqlDbType.VarChar,300),
					new SqlParameter("@Description", SqlDbType.VarChar,1000),
					new SqlParameter("@CreateTime", DbType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Key;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.Author;
            parameters[4].Value = model.Version;
            parameters[5].Value = model.Url;
            parameters[6].Value = model.SystemVersion;
            parameters[7].Value = model.Description;
            parameters[8].Value = model.CreateTime;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "insert into JNCms_Template([ID],[Key],[Title],[Author],[Version],[Url],[SystemVersion],[Description],[CreateTime]) values (@ID,@Key,@Title,@Author,@Version,@Url,@SystemVersion,@Description,@CreateTime)", parameters);
        }

        public int Update(Models.Template model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.VarChar,200),
					new SqlParameter("@Author", SqlDbType.VarChar,100),
					new SqlParameter("@Version", SqlDbType.VarChar,100),
					new SqlParameter("@Url", SqlDbType.VarChar,150),
					new SqlParameter("@SystemVersion", SqlDbType.VarChar,300),
					new SqlParameter("@Description", SqlDbType.VarChar,1000),
					new SqlParameter("@CreateTime", DbType.DateTime),
					new SqlParameter("@ID", SqlDbType.Int,8)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.Author;
            parameters[2].Value = model.Version;
            parameters[3].Value = model.Url;
            parameters[4].Value = model.SystemVersion;
            parameters[5].Value = model.Description;
            parameters[6].Value = model.CreateTime;
            parameters[7].Value = model.ID;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "update JNCms_Template set [Title]=@Title,[Author]=@Author,[Version]=@Version,[Url]=@Url,[SystemVersion]=@SystemVersion,[Description]=@Description,[CreateTime]=@CreateTime where ID=@ID", parameters);
        }

        public int Delete(int id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = id;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "delete from JNCms_Template where [ID]=@ID", parameters);
        }

        public List<Models.Template> GetList(int pageIndex, int pageSize)
        {
            List<Models.Template> list = new List<Models.Template>();
            if (pageSize > 0)
            {
                string sql;
                if (pageIndex == 1)
                    sql = pageSize.ToString();
                else
                    sql = string.Concat(Convert.ToString(pageSize * (pageIndex - 1)), ",", Convert.ToString(pageSize * pageIndex));

                using (IDataReader rd = Db.Helper.ExecuteReader(string.Concat("select * from [JNCms_Template] Order by [ID] desc limit ", sql)))
                {
                    while (rd.Read())
                    {
                        list.Add(Db.Entity.PopulateEntity<Models.Template>(rd));
                    }
                }
            }
            return list;
        }

        public Models.Template GetItem(string item)
        {
            using (IDataReader rd = Db.Helper.ExecuteReader(CommandType.Text, "select * from [JNCms_Template] where [Key]=@Key limit 1", new SqlParameter("@Key", item)))
            {
                if (rd.Read())
                {
                    return Db.Entity.PopulateEntity<Models.Template>(rd);
                }
            }
            return null;
        }
    }
}
